import matplotlib.pyplot as plt
import numpy as np

# 数据准备
tasks = ['Task1(PCA+KNN)', 'Task2(HOG+SVM)', 'Task3(CNN)']
accuracy = [92.48, 99.30, 99.50]
train_times = [0.530, 0.233, 2.116]
test_times = [0.016, 0.167, 0.057]

# 设置绘图风格
# plt.style.use('seaborn-whitegrid')

# 绘制准确率对比图
plt.figure(figsize=(8, 5))
bars = plt.bar(tasks, accuracy, color=['#4C72B0', '#55A868', '#C44E52'])
plt.ylim(80, 100)
plt.title("Accuracy Comparison", fontsize=14, pad=20)
plt.ylabel("Accuracy (%)", fontsize=12)

# 添加数据标签
for bar in bars:
    height = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2., height,
             f'{height:.2f}%',
             ha='center', va='bottom')

plt.tight_layout()
plt.savefig('accuracy_comparison.svg',format='svg')
plt.show()

# 绘制时间对比图
plt.figure(figsize=(10, 6))

# 设置柱状图位置和宽度
x = np.arange(len(tasks))
width = 0.35

# 绘制训练时间和测试时间
rects1 = plt.bar(x - width/2, train_times, width, label='Training Time', color='#4C72B0')
rects2 = plt.bar(x + width/2, test_times, width, label='Testing Time', color='#55A868')

plt.title("Time Cost Comparison", fontsize=14, pad=20)
plt.ylabel("Time (seconds)", fontsize=12)
plt.xticks(x, tasks)
plt.yscale('log')  # 使用对数坐标轴更好显示差异
plt.legend()

# 添加数据标签
def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x() + rect.get_width()/2., height*1.05,
                 f'{height:.3f}s',
                 ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

plt.tight_layout()
plt.savefig('time_comparison.svg', format='svg')
plt.show()